import 'package:base_flutter/common/colors/colors.dart';
import 'package:base_flutter/common/global.dart';
import 'package:base_flutter/pages/index.dart';
import 'package:base_flutter/pages/splash.dart';
import 'package:base_flutter/states/base_change_notifier.dart';
import 'package:base_flutter/widgets/modal_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:provider/provider.dart';

void main() => Global.init().then((value) {
      runApp(const MyApp());
    }).whenComplete(() => FlutterNativeSplash.remove());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    // Global.width = MediaQuery.of(context).size.width;
    // Global.height = MediaQuery.of(context).size.height;

    return MultiProvider(
      providers: [
        ChangeNotifierProvider<UserModel>(create: (_) => UserModel()),
        ChangeNotifierProvider<UserLocation>(create: (_) => UserLocation())
      ],
      child: Consumer2<UserModel, UserLocation>(builder: (context, user, location, child) {
        return MaterialApp(
          title: 'Demo',
          theme: ThemeData(
              primarySwatch: AppColors.primaryColor,
              primaryColor: Color(0xff333333),
              appBarTheme:
              const AppBarTheme(backgroundColor: Colors.white, elevation: 0)),

          builder: EasyLoading.init(),
          home: WillPopScope(
            child: Global.agreeClause ? const Index() : const Splash(),
            onWillPop: () async {
              return false;
            },
          ),
        );
      },),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  void _openRoute() async {
    var ret = await ModalWidget.showModal(context, Builder(builder: (context) {
      return GestureDetector(
          onTap: () {
            ModalWidget.dismiss(context, result: 123);
          },
          child: Container(
            constraints: BoxConstraints(maxHeight: 600, maxWidth: 400),
            margin: EdgeInsets.symmetric(horizontal: 20),
            color: Colors.red,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Flexible(
                    child: ListView(
                  padding: EdgeInsets.zero,
                  children: [
                    Text("data" * 300),
                  ],
                  shrinkWrap: true,
                ))
              ],
            ),
          ));
    }),
        curve: Curves.easeIn,
        modalState: ModalStates.translation,
        alignment: Alignment.center,
        touchHidden: true);
    debugInfo(ret);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times2:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _openRoute,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
